package com.shire.shire_interface.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.common.utils.MD5Util;
import com.shire.shire_interface.common.utils.ManageUtils;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.user.SystemUser;
import com.shire.shire_interface.service.user.SystemUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

//@Api(tags = "登陆")
@RestController
@RequestMapping("/user")
public class SystemUserController extends BaseController {

    @Resource
    private SystemUserService systemUserService;

//    @ApiOperation(value = "密码验证 username,password必填")
//    @ApiImplicitParams(value = {
//            @ApiImplicitParam(paramType = "body", required = true, dataType = "SystemUser", name = "user")})
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @ResponseBody
    @PostMapping("/authPass")
    public Response authPass(@RequestBody SystemUser user, HttpSession session) {
        Response result = new Response();
        String username = user.getUsername();
        String password = user.getPassword();
        SystemUser sysUser = systemUserService.getUserByUserName(username);
        if (sysUser == null) {
            result.error("该用户不存在");
            return result;
        } else {
            //密码验证
            String userpassword = MD5Util.MD5Encode(password);
            String syspassword = sysUser.getPassword();
            if (!syspassword.equals(userpassword)) {
                result.error("密码错误");
                return result;
            }
            session.setAttribute("user", sysUser);
            result.setData(sysUser);
            result.success("登录成功");
        }
        ManageUtils.saveUser(sysUser);

        return result;
    }

//    @ApiOperation(value = "用户注册 username,password必填")
//    @ApiImplicitParams(value = {
//            @ApiImplicitParam(paramType = "body", dataType = "SystemUser", name = "user")})
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @ResponseBody
    @PostMapping("/regist")
    public Response regist(@RequestBody SystemUser user) {
        Response result = new Response();
        try {
            SystemUser sysUser = systemUserService.getUserByUserName(user.getUsername());
            if (sysUser != null) {
                result.success("注册失败 用户已存在");
            } else {
                String password = user.getPassword();
                //MD5加密
                user.setPassword(MD5Util.MD5Encode(password));
                user.setCreatetime(new Date());
                user.setUpdatetime(new Date());
                systemUserService.insertSelective(user);
                result.setData(user);
                result.success("注册成功");
            }
        } catch (Exception e) {
            result.error("注册失败");
            e.printStackTrace();
        }
        return result;
    }


//    @ApiOperation(value = "修改客服密码 csId, oldPass, newPass")
    @ResponseBody
    @PostMapping("/updatePass")
    public Response updatePass(@RequestBody JSONObject jsonObject, HttpSession session) {
        Response response = new Response();

        Integer csId = jsonObject.getInteger("csId");
        String oldPass = jsonObject.getString("oldPass");
        String newPass = jsonObject.getString("newPass");

        if (!checkLoginUserByCsId(session, csId)){
            response.error("请登录！");
            response.setCode(501);
            return response;
        }

        SystemUser user = systemUserService.getUserByUserId(csId);

        if (!MD5Util.MD5Encode(oldPass).equals(user.getPassword())) {
            response.error("原密码错误，请重新输入");
            return response;
        }
        SystemUser newSysUser = new SystemUser();
        newSysUser.setId(csId);
        newSysUser.setPassword(MD5Util.MD5Encode(newPass));
        systemUserService.updateByPrimaryKeySelective(newSysUser);
        response.success("修改成功");
        return response;
    }

}
